<?php

namespace app\controller;

use app\BaseController;
use think\facade\Queue;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function step1(int $customerId)
    {
        # step 1 第一步 选中公海客户
        $url = 'https://prm-ent.xiaoshouyi.com/rest/data/v2.0/xobjects/territory/actions/checkHighSeaOperateFlow';
        $method = 'post';
        $data = [
            'dataId' => $customerId,
            'objectId' => 1,
            'xObjectApiKey' => 'claimApply__a'
        ];
        $json_data = json_encode($data);
        $headers = [];
        $headers[] = 'cookie: sensorsdata2015jssdkcross={"distinct_id":"31134905228398433045458324607266","first_id":"1900675542a1ad-0387d4bc8182904-26031b51-921600-1900675542d2cd","props":{"$latest_traffic_source_type":"引荐流量","$latest_search_keyword":"未取到值","$latest_referrer":"https://mozi-login.alibaba-inc.com/"},"$device_id":"1900675542a1ad-0387d4bc8182904-26031b51-921600-1900675542d2cd","identities":"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkxNTQwYTlkNDIyYWUtMDc0M2I4ZWM4YjlmYTNjLTI2MDMxYjUxLTkyMTYwMC0xOTE1NDBhOWQ0MzZhMCIsIiRpZGVudGl0eV9sb2dpbl9pZCI6IjMxMTM0OTA1MjI4Mzk4NDMzMDQ1NDU4MzI0NjA3MjY2In0=","history_login_id":{"name":"$identity_login_id","value":"31134905228398433045458324607266"}}; v2304LoginState=true; SESSION=YWNkZTdmYWEtZWQwYy00ZDNmLWEyYTgtYjJjMjRlZTM1ODdj; isqywxlogin=false; x-ienterprise-passport=86d40add8be44e31b863e1a8c832be533f25a34b7a3e4de7935bc11f37e9f9a7--n; x-ienterprise-tenant=3045458324607266; ssoType=oauth2; ssoKey=00APADB3CEC8C81AD3A2E7B719078E9B2E50; c-full-server-name=prm-ent.xiaoshouyi.com; resDomain=https://crm-entrs.ingageapp.com; c-user=3113490522839843; creekflowG6Designer=true; 3045458324607266isNeoWeb=true; JSESSIONID=E38FAC6E1351FABDB067613AE3B1FD27';
        $headers[] = 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
        $res = cUrl($url, $json_data, $method, 'json', $headers);
        var_dump($res);

        $step1_res = json_decode($res['data'], true);
        if ($step1_res['code'] == 200) {
            # step 2 第二步 公海客户可操作 核对公海客户信息
            $step2_res = $this->step2($customerId, $headers);
            var_dump($step2_res);
            if ($step2_res['status'] == 0) {
                # step 3 第三步 领取公海客户
                $step3_res = $this->step3($customerId, $headers);
                var_dump($step3_res);
                if (isset($step3_res['code']) && $step3_res['code'] == 200) {
                    var_dump("step3 领取成功");
                } else {
                    # 冻结期未过
                    var_dump("step3 领取失败");
                    if (!empty($step3_res['batchData'])) {
                        foreach ($step3_res['batchData'] as $batchDatum) {
                            if (!empty($batchDatum['msg'])) {
                                # 截取冻结到期时间
                                var_dump($batchDatum['msg']);
                                $msg = substr($batchDatum['msg'], -19);
                                var_dump($msg);
                            }
                        }
                    }
                }
            } else {
                exit("step2 error");
            }
        } else {
            die($step1_res['msg'] ?? 'step1 error');
        }
        exit("执行完毕");
    }

    protected function step2(int $customerId, array $headers)
    {
        $url = 'https://prm-ent.xiaoshouyi.com/json/sfa_highSea_1/open-claim.action';
        $method = 'post';
        $data = ['customizeIds' => $customerId];
        $res = cUrl($url, http_build_query($data), $method, '', $headers);
        $step2_res = json_decode($res['data'], true);
        return $step2_res;
    }

    protected function step3(int $customerId, array $headers)
    {
        $url = 'https://prm-ent.xiaoshouyi.com/json/sfa_highSea_1/claim.action';
        $method = 'get';
        $data = ['customizeIds' => $customerId];
        $res = cUrl($url, http_build_query($data), $method, '', $headers);
        $step3_res = json_decode($res['data'], true);
        return $step3_res;
    }

    public function test(int $customerId)
    {
        $url = 'https://prm-ent.xiaoshouyi.com/json/sfa_highSea_1/claim.action';
        $method = 'get';
        $data = ['customizeIds' => $customerId];
        $headers[] = 'cookie: sensorsdata2015jssdkcross={"distinct_id":"31134905228398433045458324607266","first_id":"1900675542a1ad-0387d4bc8182904-26031b51-921600-1900675542d2cd","props":{"$latest_traffic_source_type":"引荐流量","$latest_search_keyword":"未取到值","$latest_referrer":"https://mozi-login.alibaba-inc.com/"},"$device_id":"1900675542a1ad-0387d4bc8182904-26031b51-921600-1900675542d2cd","identities":"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkxNTQwYTlkNDIyYWUtMDc0M2I4ZWM4YjlmYTNjLTI2MDMxYjUxLTkyMTYwMC0xOTE1NDBhOWQ0MzZhMCIsIiRpZGVudGl0eV9sb2dpbl9pZCI6IjMxMTM0OTA1MjI4Mzk4NDMzMDQ1NDU4MzI0NjA3MjY2In0=","history_login_id":{"name":"$identity_login_id","value":"31134905228398433045458324607266"}}; v2304LoginState=true; SESSION=YWNkZTdmYWEtZWQwYy00ZDNmLWEyYTgtYjJjMjRlZTM1ODdj; isqywxlogin=false; x-ienterprise-passport=86d40add8be44e31b863e1a8c832be533f25a34b7a3e4de7935bc11f37e9f9a7--n; x-ienterprise-tenant=3045458324607266; ssoType=oauth2; ssoKey=00APADB3CEC8C81AD3A2E7B719078E9B2E50; c-full-server-name=prm-ent.xiaoshouyi.com; resDomain=https://crm-entrs.ingageapp.com; c-user=3113490522839843; creekflowG6Designer=true; 3045458324607266isNeoWeb=true; JSESSIONID=E38FAC6E1351FABDB067613AE3B1FD27';
        $headers[] = 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
        $res = cUrl($url, http_build_query($data), $method, '', $headers);
        $data = json_decode($res['data'], true);
        if (!empty($data['batchData'])) {
            foreach ($data['batchData'] as $batchDatum) {
                if (!empty($batchDatum['msg'])) {
                    $msg = substr($batchDatum['msg'], -19);
                    # 校验是否是日期时间戳
                    if (true === isValidDateFormat($msg)) {
                        var_dump('is date string');
                    } else {
                        var_dump('is not date string');
                    }
                    var_dump($msg);
                }
            }
        }
        var_dump($res);exit();
    }

    public function queue()
    {
        Queue::push('Test', ['date' => '2024-9-23 19:00:00']);

        exit("队列开始执行");
    }
}
